package com.example.schedulecard;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.schedulecard.Databases.CourseDatabaseOption;
import com.example.schedulecard.Entity.Course;

public class SeeCourseInfo extends AppCompatActivity implements View.OnClickListener {
//    定义组件
    TextView course_name,day,time,teacher,course_class,name;
    ImageView back;
//    定义按钮
    Button updata_course,delete_course;
//    定义接收信息属性
    Intent intent;
    Course course;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_see_course_info);
        Init();
    }

    private void Init(){
//        组件初始化
        course_name = (TextView) findViewById(R.id.see_course_name);
        day = (TextView) findViewById(R.id.see_course_day);
        time = (TextView) findViewById(R.id.see_course_time);
        teacher = (TextView) findViewById(R.id.see_course_teachername);
        course_class = (TextView) findViewById(R.id.see_course_class);
        name = (TextView) findViewById(R.id.see_course1);
        name.setText("课程信息");
//        接受课程信息
        intent = getIntent();
        course = (Course) intent.getSerializableExtra("courseinfo");

//        显示课程信息
        setCourseInfo(course);
        updata_course = (Button) findViewById(R.id.see_updata_button);
        delete_course = (Button) findViewById(R.id.see_delete_button);
        back = (ImageView) findViewById(R.id.see_back);
        back.setOnClickListener(this);
        updata_course.setOnClickListener(this);
        delete_course.setOnClickListener(this);
    }
//    将信息显示的方法
    private void setCourseInfo(Course course) {
        course_name.setText(course.getCourseName());
        day.setText(course.getDay() + "");
        time.setText(course.getClassStart() + "~" + course.getClassEnd() + "节课");
        teacher.setText(course.getTeacher());
        course_class.setText(course.getClassRoom());
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.see_updata_button:
                Intent intentupdata = new Intent(SeeCourseInfo.this,CourseUpdata.class);
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("courseinfo",course);
                intentupdata.putExtras(bundle1);
                startActivity(intentupdata);
                finish();
                break;
            case R.id.see_delete_button:
                SQLiteDatabase sqLiteDatabase = CourseDatabaseOption
                        .CreateCoursetable(SeeCourseInfo.this).getWritableDatabase();
                new AlertDialog.Builder(SeeCourseInfo.this).setTitle("系统提示")
//                        设置提示信息
                        .setMessage("确定删除该课程吗？")
//                        设置确定点击事件
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
//                                删除课表内的课程
                                int n = CourseDatabaseOption.deletecourse(sqLiteDatabase, course);

                                if (n > 0) {
                                    Toast.makeText(SeeCourseInfo.this, "删除成功！", Toast.LENGTH_LONG).show();
                                    Intent intent2 = new Intent(SeeCourseInfo.this,ScheduleCardHome.class);
                                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent2);
                                    finish();
                                } else {
                                    Toast.makeText(SeeCourseInfo.this, "删除失败！", Toast.LENGTH_LONG).show();
                                }
                            }
                        })
//                        设置取消点击事件
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).show();
                break;
            case R.id.see_back:
                finish();
                break;
        }
    }
}